package com.yj.suncity.activity.shouye;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class JinYinDuihuanActivity extends Activity {
	private Context context;
	private String userId;
	private boolean isShop;

	private ImageView mImg_1, mImg_2;
	private TextView mTxt_1, mTxt_2, mTxt_edt_1, mTxt_edt_2;
	private EditText mEdt_txt_1, mEdt_txt_2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_jinyin_duihuan);
		context = this;
		userId = getIntent().getStringExtra("userid");
		isShop = getIntent().getBooleanExtra("isshop", false);
		findView();
	}

	private void findView() {
		mImg_1 = (ImageView) findViewById(R.id.img_1);
		mImg_2 = (ImageView) findViewById(R.id.img_2);
		mTxt_1 = (TextView) findViewById(R.id.txt_1);
		mTxt_2 = (TextView) findViewById(R.id.txt_2);
		mTxt_edt_1 = (TextView) findViewById(R.id.txt_edt_1);
		mTxt_edt_2 = (TextView) findViewById(R.id.txt_edt_2);
		mEdt_txt_1 = (EditText) findViewById(R.id.edt_txt_1);
		mEdt_txt_2 = (EditText) findViewById(R.id.edt_txt_2);
		if (isShop) {
			((TextView) findViewById(R.id.txt_title_name)).setText("银元兑换同城币");
			findViewById(R.id.txt_shop_duihuan_hint).setVisibility(View.VISIBLE);
			mImg_1.setImageResource(R.drawable.yinyuan_golds);
			mImg_2.setImageResource(R.drawable.jin_golds);
			mTxt_1.setText("1银元");
			mTxt_2.setText("10同城币");
			mTxt_edt_1.setText("银元总数");
			mTxt_edt_2.setText("同城币总数");
			mEdt_txt_1.setHint("输入您要兑换的银元总数");
			mEdt_txt_2.setHint("可兑换同城币总数");
		} else {
			((TextView) findViewById(R.id.txt_title_name)).setText("同城币兑换银元");
			mImg_1.setImageResource(R.drawable.jin_golds);
			mImg_2.setImageResource(R.drawable.yinyuan_golds);
			mTxt_1.setText("10同城币");
			mTxt_2.setText("1银元");
			mTxt_edt_1.setText("同城币总数");
			mTxt_edt_2.setText("银元总数");
			mEdt_txt_1.setHint("请输入您要兑换的同城币总数");
			mEdt_txt_2.setHint("可兑换银元总数");
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.btn_jisuan:
			String sAllTotle = mEdt_txt_1.getText().toString().trim();
			if (sAllTotle.equals("")) {
				ToastUtil.ShowToast(context, "请输入要兑换总数！");
				return;
			}
			int allTotle = Integer.valueOf(sAllTotle);
			if (isShop) {
				mEdt_txt_2.setText(allTotle * 10 + "");
			} else {
				if (allTotle % 10 != 0) {
					ToastUtil.ShowToast(context, "请输入10的整数倍！");
					return;
				}
				mEdt_txt_2.setText(allTotle / 10 + "");
			}
			break;
		case R.id.btn_duihuan:
			String num = mEdt_txt_1.getText().toString().trim();
			if (num.equals("")) {
				ToastUtil.ShowToast(context, "请输入要兑换总数！");
				return;
			}
			String path = null;
			if (isShop) {
				path = URLCon.用户 + "?action=exchange&userid=" + userId
						+ "&silver=" + num;
			} else {
				path = URLCon.用户 + "?action=exchange_gold&userid=" + userId
						+ "&gold_num=" + num;
			}
			startDuihuan(path);
			break;
		default:
			break;
		}
	}

	private void startDuihuan(String path) {
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null, null,
				false, null, true, "正在兑换......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							ToastUtil.ShowToast(context, "兑换成功！");
						} else {
							ToastUtil.ShowToast(context,
									jsonObject.optString("msg"));
						}
					}
				});
		asyncTaskUtil.execute(path);
	}
}
